<?php

namespace App\Models\Landscaping;

use Grimzy\LaravelMysqlSpatial\Types\Polygon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;

class Landscaping extends Model
{
	use SoftDeletes;
    use SpatialTrait;
    protected $guarded = [];
    protected $spatialFields = ['areas'];

	//获取植被种类信息
    public function vegetation()
    {
    	return $this->belongsTo(\App\Models\Landscaping\Vegetation::class,'vegetation_id','id');
    }

    //查看列表--分页
    public function getList($request)
    {
        $per_page = $request->input('per_page',10);
        return self::orderBy('id','desc')
                ->with('employees')
                ->where(function($query) use ($request){
                    //检测当前的请求中是否包含park_name、principal参数
                    $park_name = $request->input('park_name', '');
                    $principal_id = $request->input('principal_id','');
                    $type = $request->input('type','');
                    $areas = $request->input('areas','');
                    if(!empty($park_name)) {
                        $query->where('park_name','like','%'.$park_name.'%');
                    }
                    if(!empty($type)) {
                        $query->where('type','=',$type)->orWhere('type','=','');
                    }
                    if(!empty($principal_id)) {
                        $query->where('principal_id','=',$principal_id);
                    }
                    if(!empty($areas)){
                        $areas = Polygon::fromString($areas);
                        $query->distance('areas',$areas,10);
                    }
                })
                ->paginate($per_page);
    }

    //查看列表--不分页
    public function getData($request)
    {
        $per_page = $request->input('per_page',10);
        return self::orderBy('id','desc')
                ->with('employees')
                ->where(function($query) use ($request){
                    //检测当前的请求中是否包含park_name、principal参数
                    $park_name = $request->input('park_name', '');
                    $principal_id = $request->input('principal_id','');
                    $areas = $request->input('areas','');
                    if(!empty($park_name)) {
                        $query->where('park_name','like','%'.$park_name.'%');
                    }
                    if(!empty($principal_id)) {
                        $query->where('principal_id','=',$principal_id);
                    }
                    if(!empty($areas)){
                        $areas = Polygon::fromString($areas);
                        $query->distance('location',$areas,10);
                    }
                })
                ->get();
    }

    //地图查询
    public function findForList($request)
    {
    	return self::where(function($query) use ($request){
                    //检测当前的请求中是否包含park_name、principal参数
    				$park_name = $request->input('park_name');
			        $park_number = $request->input('park_number');
			        $park_address = $request->input('park_address');
                    $areas = $request->input('areas','');
                    if(!empty($park_name)) {
                        $query->where('park_name','like','%'.$park_name.'%');
                    }
                    if(!empty($park_number)) {
                    	$query->where('park_number','=',$park_number);
                    }
                    if(!empty($park_address)) {
                        $query->where('park_address','like','%'.$park_address.'%');
                    }
                    if(!empty($areas)){
                        $areas = Polygon::fromString($areas);
                        $query->distance('location',$areas,10);
                    }
                })->get();
    }

    //获取园林工养护内容预设信息
    public function parkProtect()
    {
        return $this->hasMany(\App\Models\Landscaping\ParkProtect::class,'landscaping_id','id');
    }

    //查看负责人
    public function employees()
    {
        return $this->belongsTo(\App\Models\SystemManagement\Department::class,'principal_id','id')->select('id','name');
    }
}
